import java.util.HashMap;
import java.util.Map;

/**
 * 超市水果销售系统 - 支持动态价格与促销
 * 可在运行时修改水果价格、打折、满减规则
 */
public class FruitStore {

    // 使用 Map 存储水果价格，便于动态扩展和修改
    private Map<String, Double> fruitPrices;
    
    // 促销规则
    private boolean StrawberryOnDiscount;     // 是否开启草莓打折
    private double discountRate;                // 折扣率（如 0.8 表示 8 折）
    
    private boolean FullReductionActive;      // 是否开启满减
    private double fullAmount;                  // 满多少
    private double reduction;                   // 减多少

    /**
     * 构造函数：初始化默认价格和促销规则
     */
    public FruitStore() {
        fruitPrices = new HashMap<>();
        fruitPrices.put("apple", 8.0);
        fruitPrices.put("strawberry", 13.0);
        fruitPrices.put("mango", 20.0);

        // 默认促销：草莓8折，满100减10
        this.StrawberryOnDiscount = false;
        this.discountRate = 0.8;
        this.FullReductionActive = false;
        this.fullAmount = 100.0;
        this.reduction = 10.0;
    }

    // -------------------- 价格管理 --------------------

    /**
     * 设置水果价格
     */
    public void setPrice(String fruit, double price) {
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负数！");
        }
        fruitPrices.put(fruit, price);
    }

    /**
     * 获取水果价格
     */
    public double getPrice(String fruit) {
        Double price = fruitPrices.get(fruit);
        if (price == null) {
            throw new IllegalArgumentException("未知水果: " + fruit);
        }
        return price;
    }

    // -------------------- 促销管理 --------------------

    /**
     * 开启/关闭草莓打折
     */
    public void setStrawberryDiscount(boolean active, double rate) {
        if (rate <= 0 || rate > 1) {
            throw new IllegalArgumentException("折扣率必须在 (0,1] 之间");
        }
        this.StrawberryOnDiscount = active;
        this.discountRate = rate;
    }

    /**
     * 开启/关闭满减
     */
    public void setFullReduction(boolean active, double fullAmount, double reduction) {
        if (fullAmount < 0 || reduction < 0) {
            throw new IllegalArgumentException("满减金额不能为负数！");
        }
        if (reduction >= fullAmount) {
            throw new IllegalArgumentException("减免金额应小于满额");
        }
        this.FullReductionActive = active;
        this.fullAmount = fullAmount;
        this.reduction = reduction;
    }

    // -------------------- 计价核心方法 --------------------

    /**
     * 计算总价（支持动态规则）
     */
    public double calculateTotalPrice(int apple, int strawberry, int mango) {
        validateInput(apple, strawberry, mango);

        double total = 0.0;

        // 苹果
        total += apple * getPrice("apple");

        // 草莓（判断是否打折）
        double strawberryPrice = getPrice("strawberry");
        if (StrawberryOnDiscount) {
            strawberryPrice *= discountRate;
        }
        total += strawberry * strawberryPrice;

        // 芒果
        total += mango * getPrice("mango");

        // 满减判断
        if (FullReductionActive && total >= fullAmount) {
            total -= reduction;
        }

        return total;
    }

    /**
     * 输入校验
     */
    private void validateInput(int... weights) {
        for (int weight : weights) {
            if (weight < 0) {
                throw new IllegalArgumentException("水果斤数不能为负数！");
            }
        }
    }

    // -------------------- 测试主函数 --------------------

    public static void main(String[] args) {
        FruitStore store = new FruitStore();

        System.out.println("=== 超市水果销售系统（支持动态配置）测试 ===\n");

        // 场景1：顾客A，无促销
        System.out.println("场景1：顾客A - 仅苹果和草莓，无促销");
        double totalA = store.calculateTotalPrice(3, 2, 0);
        System.out.printf("苹果3斤 + 草莓2斤 = %.2f 元（预期：8*3 + 13*2 = 50）✅\n\n", totalA);

        // 场景2：顾客B，新增芒果，无促销
        System.out.println("场景2：顾客B - 三种水果，无促销");
        double totalB = store.calculateTotalPrice(2, 3, 1);
        System.out.printf("苹果2斤 + 草莓3斤 + 芒果1斤 = %.2f 元（预期：16+39+20=75）✅\n\n", totalB);

        // 场景3：顾客C，开启草莓8折
        store.setStrawberryDiscount(true, 0.8);
        System.out.println("场景3：顾客C - 草莓8折");
        double totalC = store.calculateTotalPrice(2, 3, 1);
        double expectedC = 2*8 + 3*13*0.8 + 1*20; // 16 + 31.2 + 20 = 67.2
        System.out.printf("总价：%.2f 元（预期：%.2f）✅\n\n", totalC, expectedC);

        // 场景4：顾客D，开启满100减10
        store.setFullReduction(true, 100.0, 10.0);
        System.out.println("场景4：顾客D - 草莓8折 + 满100减10");
        double totalD = store.calculateTotalPrice(5, 5, 2);
        double subtotal = 5*8 + 5*13*0.8 + 2*20; // 40 + 52 + 40 = 132
        double expectedD = subtotal - 10;
        System.out.printf("购买：苹果5斤，草莓5斤，芒果2斤\n");
        System.out.printf("打折后 %.2f 元，满减后：%.2f 元（预期：%.2f）✅\n\n", subtotal, totalD, expectedD);

        // 动态修改演示
        System.out.println("动态修改演示：");
        store.setPrice("mango", 18.0); // 芒果降价
        store.setStrawberryDiscount(true, 0.7); // 草莓改为7折
        store.setFullReduction(true, 80.0, 8.0); // 满80减8

        double totalE = store.calculateTotalPrice(2, 4, 1);
        double subE = 2*8 + 4*13*0.7 + 1*18; // 16 + 36.4 + 18 = 70.4
        double finalE = subE >= 80 ? subE - 8 : subE; // 70.4 < 80，不减
        System.out.printf("新规则下：苹果2斤，草莓4斤，芒果1斤（芒果18元，草莓7折，满80减8）\n");
        System.out.printf("总价：%.2f 元（未满80，不减）✅\n", totalE);

        System.out.println("\n✅ 所有测试通过！系统支持动态配置！");
    }
}